/**
 *
 */
package hichecker.ui.custom_views.passing;

import hichecker.controller.Config;
import hichecker.ui.components.icon_factory.IconFactory;
import hichecker.ui.components.icon_factory.IconFactory.IconType;
import hichecker.university.Requirement;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;

/**
 *
 */
public class PassingPanel extends JPanel {

	private Requirement requirement;
	private PassingCondition passingCondition;

	/**
	 *
	 */
	public PassingPanel(Requirement requirement) {
		this.requirement = requirement;
		this.passingCondition = requirement.getPassingCondition();

		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setBorder(BorderFactory.createEmptyBorder());
		createView(passingCondition);
	}

	private void createView(PassingCondition condition) {
		// Remove all components
		removeAll();

		// Create the view recursive
		createViewForCondition(this, condition);

		add(Box.createVerticalGlue());
		// Validate components
		validate();
		repaint();
	}

	private void createViewForCondition(JPanel reference, PassingCondition condition) {
		// No conditions set yet
		if (condition == null) {
			add(createButtonBar(null, null));
			return;
		}

		// Condition is a statement the add the value to the reference
		if (condition instanceof StatementCondition) {
			reference.add(createValueBar(condition));
			return;
		}

		// Condition is an OR or AND block so add each of it's components to a new panel
		if (condition instanceof AndCondition || condition instanceof OrCondition) {
			JPanel panel = new JPanel();
			panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
			panel.add(createButtonBar(condition, condition.getName()));
			panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10,
					10, 10, 10), BorderFactory.createLineBorder(Color.GRAY))); // TODO get the right color
			for (PassingCondition cond : condition.getConditions()) {
				createViewForCondition(panel, cond);
			}
			reference.add(panel);
		}
	}

	private JToolBar createValueBar(final PassingCondition reference) {
		StatementToolBar toolbar = new StatementToolBar(requirement);
		setDimension(toolbar);
		return toolbar;
	}

	private JToolBar createButtonBar(final PassingCondition reference, String caption) {
		JButton removeButton = IconFactory.getIconButton("R", Config.TOOLTIP_REMOVE_CONDITION, IconType.REMOVE, new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				passingCondition.removeCondition(reference);
				createView(passingCondition);
			}
		});

		JButton addAndButton = IconFactory.getIconButton("A", Config.TOOLTIP_ADD_AND, IconType.ADD_AND, new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (reference == null)
					passingCondition = new AndCondition();
				else
					reference.addCondition(new AndCondition());
				createView(passingCondition);
			}
		});

		JButton addOrButton = IconFactory.getIconButton("O", Config.TOOLTIP_ADD_OR, IconType.ADD_OR, new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (reference == null)
					passingCondition = new OrCondition();
				else
					reference.addCondition(new OrCondition());
				createView(passingCondition);
			}
		});

		JButton addStatementButton = IconFactory.getIconButton("S", Config.TOOLTIP_ADD_STATEMENT, IconType.ADD_STATEMENT, new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (reference == null)
					passingCondition = new StatementCondition();
				else
					reference.addCondition(new StatementCondition());
				createView(passingCondition);
			}
		});

		JToolBar toolbar = new JToolBar();
		toolbar.setFloatable(false);
		JLabel label = new JLabel(caption);
		label.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
		toolbar.add(removeButton);
		toolbar.add(label);
		toolbar.add(Box.createHorizontalGlue());
		toolbar.add(addAndButton);
		toolbar.add(addOrButton);
		toolbar.add(addStatementButton);
		setDimension(toolbar);

		return toolbar;
	}

	private void setDimension(JComponent comp) {
		Dimension max = comp.getMaximumSize();
		Dimension pref = comp.getPreferredSize();
		Dimension d = new Dimension((int) max.getWidth(), (int) pref.getWidth());
		comp.setMaximumSize(d);

	}

}
